#!/usr/bin/perl -w

 ###################################################################################
#                                                                                   #
#  Copyright 2010 Cedric Priscal                                                    ##
#                                                                                   ##
#  This file is part of Octopus Development Kit.                                    ##
#                                                                                   ##
#  Octopus Development Kit is free software: you can redistribute it and/or         ##
#  modify it under the terms of the GNU General Public License as                   ##
#  published by the Free Software Foundation, either version 3 of the               ##
#  License, or (at your option) any later version.                                  ##
#                                                                                   ##
#  Octopus Development Kit is distributed in the hope that it will be useful,       ##
#  but WITHOUT ANY WARRANTY; without even the implied warranty of                   ##
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    ##
#  GNU General Public License for more details.                                     ##
#                                                                                   ##
#  You should have received a copy of the GNU General Public License                ##
#  along with Octopus Development Kit. If not, see <http://www.gnu.org/licenses/>.  ##
#                                                                                   ##
######################################################################################
 ####################################################################################

use strict;
use warnings;
use File::Basename;

sub get_c_array($) {
    my $filename = shift;
    open(my $in, '<', $filename) or die "Cannot open file $filename ($!)";
    binmode($in);
    my $filesize = (stat($in))[7];
    my $out = "";
    $out.= "static const unsigned char array[$filesize] = {\n";
    my $buffer;
    while(my $s = read($in, $buffer, 32)) {
        my @l = unpack('C*', $buffer);
        $out.="\t".join(', ', @l).",\n";
    }
    close($in);
    $out.= "};\n";
    return $out;
}

sub compile_resource($) {
    my $filename = shift;
    my $classname = 'wxImage'.basename($filename, (".png"));
    # Create .cpp file
    open(my $out, '>', $classname.'.cpp');
    print $out "// This file has been autogenerated, do not modify it.\n";
    print $out "\n";
    print $out "#include <wx/mstream.h>\n";
    print $out "#include \"".$classname.".h\"\n";
    print $out "\n";
    print $out get_c_array($filename);
    print $out "\n";
    print $out "static wxMemoryInputStream stream(array, sizeof(array));\n";
    print $out "\n";
    print $out $classname.'::'.$classname."(void) : wxImage(stream) {\n";
    print $out "}\n";
    print $out "\n";
    close($out);
    # Create .h file
    open($out, '>', $classname.'.h');
    print $out "// This file has been autogenerated, do not modify it.\n";
    print $out "\n";
    print $out "#ifndef ".$classname."_H\n";
    print $out "#define ".$classname."_H\n";
    print $out "\n";
    print $out "#include <wx/image.h>\n";
    print $out "\n";
    print $out "class $classname: public wxImage {\n";
    print $out "\tpublic:\n";
    print $out "\t\t$classname(void);\n";
    print $out "};\n";
    print $out "\n";
    print $out "#endif\n";
    print $out "\n";
    close($out);
}

foreach my $png (glob('*.png')) {
    compile_resource($png);
}

# Rebuild RemoteBrowserFrame.cpp and RemoteBrowserFrame.h with wxFormBuilder
`wxformbuilder -g RemoteBrowserFrame.fbp`;

my $images_members;
my $images_includes;
my $out;
my $fh;

# Read cpp generated by wxFormBuilder
open($fh, '<', 'RemoteBrowserFrame.cpp');
$out='';
while(my $line = <$fh>) {
    if ($line =~ s/wxBitmap\( wxT\("(.*?)\.png"\), wxBITMAP_TYPE_ANY \)/wxBitmap\( m_wxImage$1 \)/) {
        my $classname = "wxImage$1";
        my $imagefile = "$1.png";
        my $headerfile = "$classname.h";
        $images_includes.="#include \"$headerfile\"\n";
        $images_members.="\t\t$classname\tm_$classname;\n";
    }
    $out.=$line;
}
close($fh);
# Write modified cpp
open($fh, '>', 'RemoteBrowserFrame.cpp');
print $fh $out;
close($fh);

# Read header file generated by wxFormBuilder
open($fh, '<', 'RemoteBrowserFrame.h');
$out='';
while(my $line = <$fh>) {
    $out.=$line;
    if ($line =~ /#include <wx\/frame.h>/) {
        $out.=$images_includes;
    } elsif ($line =~ /private:/) {
        $out.=$images_members;
    }
}
close($fh);
# Write modified header
open($fh, '>', 'RemoteBrowserFrame.h');
print $fh $out;
close($fh);

print "done.\n";
